package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.adapter.MyChatListAdapter;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.ChatEntity;
import com.sondon.mayi.entity.RespondBaseEntity;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

@EActivity(R.layout.mychat)
public class MyChat extends BaseActivity {
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	ListView chatlist;//消息列表
	@ViewById
	EditText chat_content;//内容
	@ViewById
	Button chat_send;//发送
	
	@Bean
	MyChatListAdapter adapter;
	
	@AfterViews
	public void initView(){
		topBar.setTopBarClickListener(new TopBarClickListener() {
			@Override
			public void rightBtnClick() {
			}
			
			@Override
			public void leftBtnClick() {
				onBackPressed();
			}
		});
		//设置适配器
		chatlist.setAdapter(adapter);
		//请求获取建立列表
		GetSugList();
	}
	
 	@Click({R.id.chat_send})
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.chat_send:
			String content=chat_content.getText().toString().trim();
			if(!TextUtils.isEmpty(content)){
				RequestParams params=new RequestParams();
				params.put("title", "");
				params.put("content", content);
				AsyncHttpUtil.post(URLConfigs.AddSuggestion_ukey+myprefs.Ukey().get(), params, new TextHttpResponseHandler() {
					
					@Override
					public void onSuccess(int status, Header[] arg1, String json) {
						LogUtil.e(TAG, "suggestlist  json :"+json);
						RespondBaseEntity entity =GsonUtil.GetFromJson(json, RespondBaseEntity.class);
						if(entity.isStatus()){
							chat_content.setText("");
							GetSugList();
						}else{
							ToastUtils.show(context, entity.getMsg());
						}
					}
					
					@Override
					public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
						ToastUtils.show(context, R.string.network_unavailable);
					}
				});
			}else{
				ToastUtils.show(context, "内容不能为空!");
			}
			break;

		default:
			break;
		}
	}
 	
 	//请求获取建立列表
 	public void GetSugList(){
		AsyncHttpUtil.get(URLConfigs.SuggestionList_ukey+myprefs.Ukey().get(), null, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String json) {
				LogUtil.e(TAG, "suggestlist  json :"+json);
				if(status==200){
					ChatEntity entity=GsonUtil.GetFromJson(json, ChatEntity.class);
					if(entity.isStatus()){
						adapter.setData(entity.getList());
						//刷新到底部 listview滚动到底部
						chatlist.setSelection(ListView.FOCUS_DOWN);
					}
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
				ToastUtils.show(context, R.string.network_unavailable);
			}
		});
 	}
}
